package com.csii.mobilebanking.expandablelistview.fragment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.csii.mobilebanking.R;
import com.csii.mobilebanking.expandablelistview.adapter.CustomerListViewAdapter;
import com.csii.mobilebanking.expandablelistview.adapter.QueryListViewAdapter;
import com.csii.mobilebanking.expandablelistview.entity.CustomerInfo;
import com.csii.mobilebanking.expandablelistview.entity.PotentialCustomer;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

/**
 * CustomerInformationManagementFragment 客户信息管理
 * 
 * @author hua
 * 
 */
@SuppressLint("Recycle")
public class CustomerIMFragment extends BaseFragment implements OnClickListener {
	private static final String MAIN_ACTIVITY = "CustomerIMtFragment";
	private ListView lv_query;
	private List<PotentialCustomer> data;
	private ListView lv_iinfo_list;
	private List<CustomerInfo> dataInfo;

	private Button btn_info_query;
	private Spinner sp_idcard_type;
	private Button btn_cim_create;//  新建

	@SuppressWarnings("static-access")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.d(MAIN_ACTIVITY, "onCreateView_CustomerIMtFragment--"
				+ this.getArguments().getInt("index"));

		initView(inflater, R.array.title_cim, R.array.title_cim__tag,
				R.array.title_cim_ids, R.array.title_cim_layout,
				R.array.tab_cim_view_ids);
		
		//初始化tabweight
		tabHost.inflate(getActivity(), R.layout.customer_select_info, tabHost.getTabContentView());
		tabHost.addTab(tabHost.newTabSpec("select_info").setContent(R.id.tab_cim_view_select_info).setIndicator("select_info"));
		
		

		if (container == null)
			return null;
		
		//潜在用户信息设置
		lv_query = (ListView) tabHost.findViewById(R.id.lv_query);

		View view = inflater.inflate(R.layout.customer_listview_item, null);

		// 设置标题
		String[] titles = getResources().getStringArray(R.array.pc_title);
		TextView tv_name = (TextView) view.findViewById(R.id.tv_cim_name);
		tv_name.setText(titles[0]);
		TextView tv_sex = (TextView) view.findViewById(R.id.tv_cim_sex);
		tv_sex.setText(titles[1]);
		TextView tv_source = (TextView) view.findViewById(R.id.tv_cim_source);
		tv_source.setText(titles[2]);
		TextView tv_creator = (TextView) view.findViewById(R.id.tv_cim_creator);
		tv_creator.setText(titles[3]);
		TextView tv_createTime = (TextView) view
				.findViewById(R.id.tv_cim_createTime);
		tv_createTime.setText(titles[4]);
		lv_query.addHeaderView(view, null, false);

		data = new ArrayList<PotentialCustomer>();
		for (int i = 0; i < 4; i++) {
			PotentialCustomer customer = new PotentialCustomer();
			customer.setCreateTime(new Date());
			customer.setCreator("李雪");
			customer.setName("李华");
			customer.setSex("男");
			customer.setSource("商户推荐");
			data.add(customer);
		}
		QueryListViewAdapter adapter = new QueryListViewAdapter(getActivity(),
				data);
		lv_query.setAdapter(adapter);
		
		lv_query.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				tabHost.setCurrentTabByTag("select_info");
				
			}
		});
		
		
		
		
		
		
		//证件类型
		
		sp_idcard_type=(Spinner) tabHost.findViewById(R.id.sp_idcard_type);
		
		ArrayAdapter<String> simpleAdapter=new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, R.id.tv_idcard_item, getResources().getStringArray(R.array.sp_idcard_type));
		sp_idcard_type.setAdapter(simpleAdapter);	
			
		//新建
		btn_cim_create=(Button) tabHost.findViewById(R.id.btn_cim_create);
		btn_cim_create.setOnClickListener(this);
		
		
		
		//客户信息设置
		lv_iinfo_list = (ListView) tabHost.findViewById(R.id.lv_info_list);

		View headView = inflater
				.inflate(R.layout.customer_info_list_item, null);
		String[] titles_info = getResources()
				.getStringArray(R.array.info_title);
		TextView tv_info_name = (TextView) headView
				.findViewById(R.id.tv_cim_info_name);
		tv_info_name.setText(titles_info[0]);
		TextView tv_info_id = (TextView) headView
				.findViewById(R.id.tv_cim_info_id);
		tv_info_id.setText(titles_info[1]);

		TextView tv_info_cardType = (TextView) headView
				.findViewById(R.id.tv_cim_info_cardType);
		tv_info_cardType.setText(titles_info[2]);

		TextView tv_info_idNumber = (TextView) headView
				.findViewById(R.id.tv_cim_info_idNumber);
		tv_info_idNumber.setText(titles_info[3]);

		TextView tv_info_rating = (TextView) headView
				.findViewById(R.id.tv_cim_info_rating);
		tv_info_rating.setText(titles_info[4]);

		TextView tv_info_advisor = (TextView) headView
				.findViewById(R.id.tv_cim_info_advisor);
		tv_info_advisor.setText(titles_info[5]);

		lv_iinfo_list.addHeaderView(headView, null, false);

		dataInfo = new ArrayList<CustomerInfo>();
		for (int i = 0; i < 4; i++) {
			CustomerInfo info = new CustomerInfo();
			info.setId("222222552");
			info.setCardType("身份证");
			info.setIdNumber("43333332323232323");
			info.setName("admin");
			info.setAdvisor("Hua");
			info.setRating("白银");
			dataInfo.add(info);
		}
		CustomerListViewAdapter listViewAdapter = new CustomerListViewAdapter(
				getActivity(), dataInfo);
		lv_iinfo_list.setAdapter(listViewAdapter);
		
		btn_info_query = (Button) tabHost.findViewById(R.id.btn_cim_info_query);
		btn_info_query.setOnClickListener(this);
		
		//注册上下文菜单
		registerForContextMenu(lv_iinfo_list);
		
		
		return tabHost;
	}

	/***
	 * title 菜单
	 */
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);

	}

	/***
	 * 点选按钮监听事件
	 */
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		Log.d(MAIN_ACTIVITY, "checkedId..." + checkedId);
		switch (checkedId) {
		case R.id.rbtn_title_cim_info:
			tabHost.setCurrentTab(0);
			break;
		case R.id.rbtn_title_cim_create:
			tabHost.setCurrentTab(1);
			break;
		case R.id.rbtn_title_cim_slect:
			tabHost.setCurrentTab(2);
			break;
		case R.id.rbtn_title_cim_other:
			tabHost.setCurrentTab(3);
			break;
		}
	}

	@Override
	public void onClick(View view) {
		Log.d("tag.", view.getId()+"---"+R.id.btn_cim_info_query+"--");
		switch (view.getId()) {
		case R.id.btn_cim_info_query:
			if(dataInfo.size()>0)
				lv_iinfo_list.setVisibility(View.VISIBLE);
			break;
		case R.id.btn_cim_create:
			//新建按钮
			tabHost.setCurrentTab(0);
			break;
		default:
			break;
		}

	}
	
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		
		menu.add(0, 0, 0, "详细信息");
		menu.add(0, 0, 0, "卡折信息");
		menu.add(0, 0, 0, "持有产品");
		menu.add(0, 0, 0, "签约信息");
		menu.add(0, 0, 0, "风险评估");
		
		
	}
}